package com.ce.framework.datamaintain.logic;

import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;

import com.ce.framework.constants.WebConstants;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.BeanAccessUtil;
import com.ce.framework.util.LogUtil;
import com.ce.framework.util.StringUtil;
import com.ce.util.RegeditUtil;



/**
 * 嫦娥软件有限公司 版权所有 2013
 * 创建人： andy
 * 创建时间： Sep 7, 2010 11:32:38 AM
 * 功能描述：分页设置类
 * ==================================
 * 修改历史
 * 修改人        修改时间      修改位置（函数名）
 *
 * ==================================
 */
public class DivideInfo implements Serializable {
	private static final long serialVersionUID = 1L;

	private int rows = 12;  //当页的记录条数

	private int pageNo = 1;

	private int rowAmount;

	public int getBegRowNo(){
		return (pageNo - 1) * this.rows;
	}

	public int getEndRowNo(){
		return pageNo * this.rows;
	}

	public int getPageAmount() {
//		System.out.print("===========================");
//		long tt = Math.round(((double)rowAmount)/rows+0.499999);
//		return Math.max(tt, 1);
//		return tt==0?1:tt;
//		long tt = Math.round(((double)rowAmount)/rows+0.5);
//		return tt;
		
		return (int) Math.max(Math.round(((double)rowAmount)/rows+0.499999), 1);
	}

	public static DivideInfo getNewInstance(HttpServletRequest request)	throws AppException
	{
		DivideInfo divideRequest = null;
		if(request.getSession().getAttribute(WebConstants.LAST_MODIFY_FLAG)!=null && 
				request.getSession().getAttribute(WebConstants.LAST_DIVIDE_PAGE_INFO)!=null){
			divideRequest = (DivideInfo) request.getSession().getAttribute(WebConstants.LAST_DIVIDE_PAGE_INFO);
			request.getSession().removeAttribute(WebConstants.LAST_MODIFY_FLAG);
			return divideRequest;
		}
		divideRequest = new DivideInfo();
		try {
			String strTmp = request.getParameter(WebConstants.SEARCH_PAGE_SIZE_FLDNAME);//先取请求参数
			if(StringUtil.isBlank(strTmp))
				strTmp = (String) request.getAttribute(WebConstants.SEARCH_PAGE_SIZE_FLDNAME);//无请求参数时再检查request中暂存数据
			if(StringUtil.isBlank(strTmp))
				strTmp = (String) request.getSession().getAttribute(request.getAttribute("REQUEST_PATH").toString());//如果仍未找到则检查SESSION中当前页面分页数
			if(StringUtil.isBlank(strTmp))
				strTmp = (String) request.getSession().getAttribute(WebConstants.SEARCH_PAGE_SIZE_FLDNAME);//最后检查SESSION中的分页数
			if(StringUtil.isBlank(strTmp)){//全部未指定分页数时获取系统注册表设置的第一个分页数
				strTmp = RegeditUtil.getRegeditStringValue(com.ce.util.RegeditUtil.SYSTEM_PAGE_LIST).split(",")[0];
			}
			if(StringUtil.isBlank(strTmp)){
				strTmp = WebConstants.SEARCH_PAGE_SIZE_FLDNAME; //都未指定每页记录数时则设置默认值20每页
			}
			int num = 0;
			try {
				num = Integer.parseInt(strTmp);
				if(StringUtil.isNotBlank(request.getParameter(WebConstants.SEARCH_PAGE_SIZE_FLDNAME))){//如果用户指定了新的分页数，则存入SESSION
					request.getSession().setAttribute(request.getAttribute("REQUEST_PATH")+WebConstants.SEARCH_PAGE_SIZE_FLDNAME, ""+num);
				}
			} catch (Exception e) {}
			if (num>0) {
				divideRequest.setRows(num);
			} else {
				divideRequest.setRows(WebConstants.SEARCH_PAGE_SIZE);
			}
			strTmp = request.getParameter(WebConstants.SEARCH_REQUEST_PAGENO_FLDNAME);
			try {
				num = 0;
				num = Integer.parseInt(strTmp);
			} catch (Exception e) {}
			if (num>0) {
				divideRequest.setPageNo(num);
			}
		} catch (Exception ex) {
			LogUtil.logError(ex);
			divideRequest = new DivideInfo();
		}
		return divideRequest;
	}
	
	public DivideInfo(){
		
	}

	public DivideInfo(HttpServletRequest request){

		try {
			String strTmp = request.getParameter(WebConstants.SEARCH_PAGE_SIZE_FLDNAME);
			if(StringUtil.isBlank(strTmp))
				strTmp = (String) request.getAttribute(WebConstants.SEARCH_PAGE_SIZE_FLDNAME);
			if (strTmp != null) {
				this.setRows(Integer.parseInt(strTmp));
			} else {
				this.setRows(WebConstants.SEARCH_PAGE_SIZE);
			}
			strTmp = request.getParameter(WebConstants.SEARCH_REQUEST_PAGENO_FLDNAME);
			if (strTmp != null) {
				this.setPageNo(Integer.parseInt(strTmp));
			}
		} catch (Exception ex) {
			
		}
	}
	
	public Object clone(){
		
		DivideInfo obj = new DivideInfo();
        try {
			BeanAccessUtil.copyBeanProperties(obj,this);
		} catch (AppException e) {
			e.printStackTrace();
		}
		return obj;
	}

	public int getPageNo() {
		return Math.min(pageNo,this.getPageAmount());
	}

	public void setPageNo(int pageNo) {
		if(pageNo<=0){
			pageNo = 1;
		}
		this.pageNo = pageNo;
	}

	public int getRowAmount() {
		return rowAmount;
	}

	public void setRowAmount(int rowAmount) {
		this.rowAmount = rowAmount;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

}
